package com.example.guet_assistant.handler;

import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import com.fasterxml.jackson.databind.ObjectMapper;

public class WebSocketHandler extends TextWebSocketHandler {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // 处理客户端发送的消息（如果需要）
        System.out.println("Received message: " + message.getPayload());
    }

    public void sendMessage(WebSocketSession session, Object message) throws Exception {
        String jsonMessage = objectMapper.writeValueAsString(message);  // 将对象转为 JSON
        session.sendMessage(new TextMessage(jsonMessage));  // 发送 JSON 消息到客户端
    }
}
